<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="摘要标题" prop="educationTitle">
        <el-input
          v-model="queryParams.educationTitle"
          placeholder="请输入摘要标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="摘要状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_notice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="educationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        :index="table_index"
        width="50"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="摘要标题"
        align="center"
        prop="educationTitle"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="摘要图片"
        align="center"
        prop="educationUrl"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.educationUrl"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_notice_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建者"
        align="center"
        prop="createBy"
        width="100"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="修改者"
        align="center"
        prop="updateBy"
        width="100"
      />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleContent(scope.row)"
            >查看内容
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--查看公告内容-->
    <el-dialog :visible.sync="openContent" width="780px" append-to-body>
      <el-form ref="form" :model="form">
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-tag size="medium" style="font-size: 15px">{{
                form.educationTitle
              }}</el-tag>
            </el-form-item>
          </el-col>

          <el-col :span="24" style="margin-top: -22px">
            <el-divider
              ><i class="el-icon-star-off" style="margin-right: 3px"></i
              >摘要标题</el-divider
            >
          </el-col>
          <el-col :span="24" style="margin-top: 10px">
            <el-form-item>
              <el-card>
                <el-col>
                  <div
                    v-html="form.educationContent"
                    class="content demo-image__lazy"
                  ></div>
                </el-col>
              </el-card>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 添加或修改公告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
      align="center"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="摘要标题" prop="educationTitle">
              <el-input
                v-model="form.educationTitle"
                placeholder="请输入摘要标题"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_notice_status"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="摘要图片">
              <image-upload v-model="form.educationPicture" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="educationContent">
              <editor v-model="form.educationContent" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listEducation,
  getEducation,
  delEducation,
  addEducation,
  updateEducation,
} from "@/api/information/education";

export default {
  name: "Education",
  dicts: ["sys_notice_status", "sys_notice_type"],
  data() {
    return {
      urlList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      educationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openContent: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        educationTitle: undefined,
        createBy: undefined,
        updateBy:undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        educationTitle: [
          { required: true, message: "摘要标题不能为空", trigger: "blur" },
        ],
        educationContent: [
          { required: true, message: "摘要内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //序号换页问题
    table_index(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    // 富文本校验
    contentRule() {
      this.$refs.addModelRef.validateField("educationContent");
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listEducation(this.queryParams).then((response) => {
        this.educationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        educationId: undefined,
        educationTitle: undefined,
        educationContent: undefined,
        educationPicture: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noticeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加摘要";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const educationId = row.educationId || this.ids;
      getEducation(educationId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改摘要";
      });
    },

    /** 查询公告内容按钮操作 */
    handleContent(row) {
      this.reset();
      const educationId = row.educationId || this.ids;
      getEducation(educationId).then((response) => {
        this.form = response.data;
        if (this.form.educationUrl != null) {
          this.urlList = this.form.educationUrl.split(",");
        }
        this.openContent = true;
        this.title = row.educationTitle;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.educationId != undefined) {
            if (this.form.educationPicture == "") {
              this.form.educationPicture = null;
            }
            updateEducation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.form.educationPicture == "") {
              this.form.educationPicture = null;
            }
            addEducation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const educationIds = row.educationId || this.ids;
      this.$modal
        .confirm('是否确认删除摘要编号为"' + educationIds + '"的数据项？')
        .then(function () {
          return delEducation(educationIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
  <style>
.demo-image__lazy {
  height: 600px;
  overflow-y: auto;
}
.demo-image__lazy .el-image {
  display: block;
  min-height: 200px;
  margin-bottom: 10px;
}
.demo-image__lazy .el-image:last-child {
  margin-bottom: 0;
}
.el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}
.content {
  text-indent: 2em;
}
</style>